﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ZD_Utility.ZD_WebRequest
{
    public class ZDRequestBase : IDisposable
    {
        #region 属性
        private string method = HttpMethod.Get;

        /// <summary>
        /// 请求Uri
        /// </summary>
        public virtual string Uri { get; set; }

        /// <summary>
        /// 请求方法类型
        /// </summary>
        public string Method { get { return method; } set { method = value; } }

        /// <summary>
        /// 请求的Content-typeHttp头
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// 当前HttpRequest请求对象
        /// </summary>
        public HttpWebRequest req { get; set; }


        /// <summary>
        /// 当前HttpRequest请求返回的WebResponse
        /// </summary>
        public WebResponse response { get; set; }

        #endregion

        #region 构造函数
        public ZDRequestBase(string uri)
        {
            this.Uri = uri;
            req = System.Net.WebRequest.Create(Uri) as HttpWebRequest;
        }
        #endregion

        /// <summary>
        /// 发送请求 返回此次请求的相应 即WebResponse对象
        /// </summary>
        /// <returns></returns>
        public WebResponse SendRequest()
        {
            if (string.IsNullOrEmpty(this.Uri))
            {
                throw new Exception("无效的Uri，请检查！");
            }

            AddHeaders(req.Headers);

            if (!string.IsNullOrEmpty(this.ContentType))
            {
                req.ContentType = this.ContentType;
            }

            if (!string.IsNullOrEmpty(this.Method))
            {
                req.Method = this.Method;
            }

            if (req.Method == HttpMethod.Post)
            {
                WriteBody();
            }

            try
            {
                response = req.GetResponse();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return response;

        }

        /// <summary>
        /// 发送请求 返回当前请求输出流的字符串形式
        /// </summary>
        /// <returns>当前请求输出流的字符串形式</returns>
        public virtual string SendReqReturnResponseString()
        {
            response = SendRequest();
            if (response == null)
                return "";
            return ReadResponse(response);
        }

        /// <summary>
        /// 发送请求 返回当前请求的输出流
        /// </summary>
        /// <returns>当前请求的输出流</returns>
        public virtual Stream SendReqReturnResponseStream()
        {
            response = SendRequest();
            return response.GetResponseStream();
        }

        /// <summary>
        /// 为POST请求添加Body参数信息
        /// </summary>
        /// <param name="reqStream"></param>
        protected virtual void WriteBody()
        {
            // Nothing to do. 
        }

        /// <summary>
        /// 为请求添加HttpHeader
        /// </summary>
        /// <param name="headcoll"></param>
        protected virtual void AddHeaders(WebHeaderCollection headcoll) { }

        /// <summary>
        /// 获取请求的返回流
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        protected string ReadResponse(WebResponse res)
        {
            string response = string.Empty;
            var stream = res.GetResponseStream();
            using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
            {
                response = reader.ReadToEnd();
            }
            res.Close();
            return response;
        }

        #region IDisposeable接口实现
        public void Dispose()
        {
            this.response.Close();
        }
        #endregion
    }
}
